// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================

package tc.crase.cmds;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;

import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;

import tc.common.registry.RegistrySys;
import tc.common.utility.GrView;
import tc.common.utility.TcCmd;
import tc.common.utility.TcDisplayXY;

import tc.crase.db.XattrGIT;
import tc.crase.db.XattrGXY;
import tc.crase.view.ViewGraph;

public class CmdMove extends CmdEx implements ActionListener {
	XattrGXY xo = null;
	Point2D.Double  xy;
	boolean xyValid = false;
		
	enum State { MOVE_SINGLE, MOVE_ALL };
	State state = State.MOVE_SINGLE;
		
	public CmdMove () {
	}
	
	public void initializeDialog() {
		super.initializeDialog();
		setLabel("Move Object");
		setHint("Select object in graph. Next point moves the object.");
		
		JRadioButton moveSingleRadio = new JRadioButton("Move Single");
		moveSingleRadio.addActionListener(this);
		moveSingleRadio.setActionCommand("move_single");
		moveSingleRadio.setAlignmentX(0.0f);
		moveSingleRadio.setPreferredSize(new Dimension(TcCmd.SIDEBAR_WIDTH,24));
		moveSingleRadio.setMaximumSize(new Dimension(TcCmd.SIDEBAR_WIDTH,24));
				
		JRadioButton moveAllRadio = new JRadioButton("Move All");
		moveAllRadio.addActionListener(this);
		moveAllRadio.setActionCommand("move_all");
		moveAllRadio.setAlignmentX(0.0f);
		moveAllRadio.setPreferredSize(new Dimension(TcCmd.SIDEBAR_WIDTH,24));
		moveAllRadio.setMaximumSize(new Dimension(TcCmd.SIDEBAR_WIDTH,24));
		
		ButtonGroup radioGroup = new ButtonGroup();
		radioGroup.add(moveSingleRadio);
		radioGroup.add(moveAllRadio);
		moveSingleRadio.setSelected(true);
		
		dialog.add(moveSingleRadio);
		dialog.add(moveAllRadio);
		
		finalizeDialog();
	}
	
	public void enterPoint(Point pt, GrView view) {
		if (view instanceof ViewGraph == false) return;
		
		TcDisplayXY PT = new TcDisplayXY(new Point2D.Double(pt.x, pt.y), view.transf, TcDisplayXY.CoordSys.DEVICE);
		PT.transform(TcDisplayXY.CoordSys.WORLD);
		
		if (state == State.MOVE_SINGLE) {
			if (xo == null) {
				xo = ViewGraph.findNearestGi(view, PT.xy);
				xy = PT.xy;
			}
			else
			{	RegistrySys.registry.lock(); {
					xo.xy.x += PT.xy.x - xy.x; 
					xo.xy.y += PT.xy.y - xy.y;
					//TODO xo.getDoc().objChanged();
					xo = null;
				} RegistrySys.registry.unlock();
			}
		}
		else {
			if (xyValid == false) {
				xyValid = true;
				xy = PT.xy;
			}
			else {
				ViewGraph viewGraph = (ViewGraph)view;
				RegistrySys.registry.lock(); {
					for (XattrGIT attrGIT : viewGraph.attrGraph) {
						XattrGXY attrGXY = attrGIT.getAsGXY();
						if (attrGXY == null) return;
					
						attrGXY.xy.x += PT.xy.x - xy.x; 
						attrGXY.xy.y += PT.xy.y - xy.y;
					}
				} RegistrySys.registry.unlock();	
				
				xyValid = false;
			}
		}
	}
	
	public void enterReset(GrView view) {
	}
	
	public void enterMove(Point pt, GrView view)  {
	}
	
	public void actionPerformed(ActionEvent e) {
		if ("move_single".equals(e.getActionCommand())) {
			state = State.MOVE_SINGLE;
			xo = null;
		}
		if ("move_all".equals(e.getActionCommand())) {
			state = State.MOVE_ALL;
			xyValid = false;
		}
		
		
	}
}
